/*
 * @Date: 2021-09-08 16:26:54
 * @Author: Acckno1
 * @LastEditTime: 2021-09-08 19:20:32
 * @Description: Dijsktra堆优化
 */
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)
#define x first
#define y second

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 10010, M = 1e6 + 10;

int n, h[N], e[M], ne[M], w[M], idx;
int line[M];  //每条边属于几号线
int stops[N]; //站点
int dist[N];  //
int cnt[N];   //记录经过的边数；换成少===经过的边数少
bool st[N];
int pre[N];
string info[N]; //记录地铁线路信息

/*
邻接表建边：建立a到b的边
a和b是站点
c是权值，表示中间隔了几站
id是线路号line
*/

void add(int a, int b, int c, int id)
{
    e[idx] = b, w[idx] = c, line[idx] = id, ne[idx] = h[a], h[a] = idx++;
}

//4位站牌号，不足补上
string get_number(int x)
{
    string res = to_string(x);
    while (res.size() < 4)
        res = '0' + res;
    return res;
}

void dijkstra(int start, int end)
{

    memset(dist, 0x3f, sizeof dist);
    memset(cnt, 0, sizeof cnt);
    memset(st, 0, sizeof st);

    dist[start] = 0, cnt[start] = 0;
    priority_queue<PII, vector<PII>, greater<PII> > heap; //小根堆
    heap.push({0, start});                                //加入起点{距离，编号}

    while (heap.size())
    {
        auto t = heap.top();
        heap.pop();

        int ver = t.y; //节点编号
        if (ver == end)
            break;
        if (st[ver])
            continue;

        st[ver] = true;

        for (int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];
            //距离更短，更新
            if (dist[j] > dist[ver] + w[i])
            {
                dist[j] = dist[ver] + w[i];
                cnt[j] = cnt[ver] + 1; //走过的边数
                pre[j] = ver;
                info[j] = "Take Line#" + to_string(line[i]) +
                          " from " + get_number(ver) + " to " + get_number(j) + ".";
                heap.push({dist[j], j});
            }
            //距离相同，看看
            else if (dist[j] == dist[ver] + w[i])
            {
                if (cnt[j] > cnt[ver] + 1)
                {
                    cnt[j] = cnt[ver] + 1;
                    pre[j] = ver;
                    info[j] = "Take Line#" + to_string(line[i]) +
                              " from " + get_number(ver) + " to " + get_number(j) + ".";
                    heap.push({dist[j], j});
                }
            }
        }
    }

    cout << dist[end] << endl;
    vector<string> path;
    //倒序存入path
    for (int i = end; i != start; i = pre[i])
        path.push_back(info[i]);

    for (int i = path.size() - 1; i >= 0; i--)
        cout << path[i] << endl;
}

inline void solution()
{
    cin >> n;
    memset(h, -1, sizeof h); //邻接表初始化

    for (int i = 1; i <= n; i++)
    {          //n条线路
        int m; //站点
        cin >> m;
        for (int i = 0; i < m; i++)
            cin >> stops[i];
        //同一条线路上的站点，因为都是连通的，两两之间建边
        for (int j = 0; j < m; j++)
        {
            for (int k = 0; k < j; k++)
            {
                int length;
                //如果不是环路
                if (stops[0] != stops[m - 1])
                    length = j - k;
                //如果是环路，取两边短的
                else
                    length = min(j - k, m - 1 - j + k);
                //建边的时候把长度也建好了，等于之间走了几站：length
                add(stops[j], stops[k], length, i);
                add(stops[k], stops[j], length, i);
            }
        }
    }

    int m;
    cin >> m;
    while (m--)
    {
        int start, end;
        cin >> start >> end;
        dijkstra(start, end);
    }
}

int main()
{
    // freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}